package com.ssl.basics.sort;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author shi_senlin
 * @create 2021/06/07 21:19
 */
public class StreamSort {
    public static void main(String[] args) {
        List<A> list = new ArrayList<>();
        A a;
        for (int i = 9; i >= 1; i--) {
            a = new A();
            a.setAge(i);
            a.setCreateDate(new Date());
            if (i % 2 == 0) {
                a.setDeleted(true);
            }
            if (i == 7) {
                a.setDeleted(false);
            }
            list.add(a);
        }
        list = sort(list);
        System.out.println(list);

    }

    public static List<A> sort(List<A> list) {

        list = list.stream().sorted(Comparator.comparing((A a) -> {
            if (null != a.getDeleted()&&a.getDeleted()){
                return -1;
            }
            return 1;
        })
        .thenComparing(Comparator.comparing(A::getAge,Comparator.reverseOrder()))
        ).collect(Collectors.toList());
        return list;
    }


    static class A {
        private int age;
        private Date createDate;
        private Boolean deleted;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Date getCreateDate() {
            return createDate;
        }

        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }

        public Boolean getDeleted() {
            return deleted;
        }

        public void setDeleted(Boolean deleted) {
            this.deleted = deleted;
        }

        @Override
        public String toString() {
            return "A{" +
                    "age=" + age +
                    ", createDate=" + createDate +
                    ", deleted=" + deleted +
                    '}';
        }
    }
}
